<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@page import="java.util.Collection" %>
<%@page import="java.util.Iterator" %>
<%@page import="com.liuya.safe.model.SafeRole" %>
<%@page import="com.liuya.safe.model.SafePrivilege" %>
<%@ include file="../common/taglib.jsp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="shortcut icon" href="${contextPath}/style/def/favicon.ico">
    <%@ include file="../common/static.jsp" %>

        <%
I18N i18n=I18N.getWebInstance( request );
%>

    <title><%=i18n.say("Role_privilege_mapping")%>
    </title>
<body>
<%
    Collection assignedPvlgs = (Collection) request.getAttribute("assignedPvlgs");
    SafeRole role = (SafeRole) request.getAttribute("role");

    String[] mainMenuHrefs = new String[]{
            "/roleMng/roleList.do"
    };
    String[] mainMenuTexts = new String[]{
            i18n.say("Role"),
    };

    request.setAttribute("mainMenuHrefs", mainMenuHrefs);
    request.setAttribute("mainMenuTexts", mainMenuTexts);
%>


<jsp:include page="../common/header.jsp"></jsp:include>
<%@include file="../common/menu.jsp" %>

<div class="smallContainer">
    <label><%=i18n.say("Role")%>
    </label>&nbsp;&nbsp;&nbsp;---&nbsp;&nbsp;&nbsp;<%=role.getName()%>
    <ul id="tree" class="tree"></ul>
    <br/>
    <input type="button" value="OK" onclick="javascript:assignRvlgs()"/>
</div>

<jsp:include page="../common/footer.jsp"></jsp:include>

<script type="text/javascript">
    var tree;

    jQuery(document).ready(function () {
        var treeSetting = {
            async: true,
            checkable: true,
            isSimpleData: true,
            treeNodeKey: "id",
            treeNodeParentKey: "pId",
            asyncUrl: "/privilegeMng/loadTree.do?oper=loadTree",
            callback: {
                asyncSuccess: afterTreeLoad
            },
            asyncParam: ["name", "id", "isLeaf"]
        };

        var treeNodes = [];
        tree = jQuery("#tree").zTree(treeSetting, treeNodes);
    });

    function afterTreeLoad(event, treeId, treeNode, msg) {
        tree.expandAll(true);

        // check up nodes
        var node;
        <%for( Iterator iter=assignedPvlgs.iterator(); iter.hasNext(); ) {
            SafePrivilege pvlg=(SafePrivilege) iter.next();
        %>
        node = tree.getNodeByParam("id", <%=pvlg.getId()%>);
        node.checked = true;
        if (node.isLeaf == '1') {
            tree.updateNode(node, true);
        } else {
            tree.updateNode(node, false);
        }
        <% } %>
    }

    function assignRvlgs() {
        var checkedNodes = tree.getCheckedNodes(true);
        var selectedItems = new Array();
        for (var i = 0; i < checkedNodes.length; i++) {
            selectedItems.push(checkedNodes[i].id);
        }

        jQuery.ajax({
            url: "assignPrivileges.do?roleId=<%=role.getId()%>",
            type: "POST",
            data: {"pvlgIds": selectedItems.join(',')},
            success: function (msg) {
                alert("<%=i18n.say( "saved" ) %>");
            }
        });
    }
</script>

</body>
</html>